package com.jfire.codejson.methodinfo.impl.write;

import java.lang.reflect.Method;
import com.jfire.codejson.util.NameTool;

public class ReturnMapMethodInfo extends AbstractWriteMethodInfo
{
    
    public ReturnMapMethodInfo(Method method)
    {
        super(method);
        str = "Map " + NameTool.getNameFromMethod(method) + " = " + getValue + ";\n";
        str += "if(" + NameTool.getNameFromMethod(method) + "!=null)\n{\n";
        str += "\tcache.append(\"\\\"" + NameTool.getNameFromMethod(method) + "\\\":{\");\n";
        str += "\tSet entries = " + NameTool.getNameFromMethod(method) + ".entrySet();\n";
        str += "\tIterator it = entries.iterator();\n";
        str += "\tjava.util.Map.Entry entry = null;\n";
        str += "\twhile(it.hasNext())\n\t{\n";
        str += "\t\tentry = it.next();\n";
        str += "\t\tif(entry.getKey()!=null && entry.getValue()!=null)\n";
        str += "\t\t{\n";
        str += "\t\t\tif(entry.getKey() instanceof String)\n";
        str += "\t\t\t{\n";
        str += "\t\t\t\tcache.append('\\\"').append((String)entry.getKey()).append(\"\\\":\");\n";
        str += "\t\t\t}\n";
        str += "\t\t\telse\n";
        str += "\t\t\t{\n";
        str += "\t\t\t\tcache.append('\\\"');\n";
        str += "\t\t\t\tWriterContext.write(entry.getKey(),cache);\n";
        str += "\t\t\t\tcache.append(\"\\\":\");\n";
        str += "\t\t\t}\n";
        str += "\t\t\tif(entry.getValue() instanceof String)\n";
        str += "\t\t\t{\n";
        str += "\t\t\t\tcache.append('\\\"').append((String)entry.getValue()).append('\\\"');\n";
        str += "\t\t\t}\n";
        str += "\t\t\telse\n";
        str += "\t\t\t{\n";
        str += "\t\t\t\tWriterContext.write(entry.getValue(),cache);\n";
        str += "\t\t\t}\n";
        str += "\t\t\tcache.append(',');\n";
        str += "\t\t}\n";
        str += "\t}\n";
        str += "}\n";
        str += "if(cache.isCommaLast()){cache.deleteLast();}\n";
        str += "cache.append(\"},\");\n";
    }
    
}
